package easyjava.hibernate.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import easyjava.hibernate.bean.Accommodation;


public class AccommodationDao {
	private Session session;

	public AccommodationDao(Session session) {
		this.session = session;
	}

	public List<Accommodation> list() {
		Query<Accommodation> query = session.createQuery("from Accommodation", Accommodation.class);
		return query.getResultList();
	}
    public Accommodation getAccommodationById(int id){
    	return session.get(Accommodation.class, id);
    }
    public List<Accommodation> getAccommodationByCreateTime(Date createTime){
    	Query<Accommodation> query=session.createQuery("from Accommodation where createDateTime=:createTime" ,Accommodation.class);
    	query.setParameter("createTime", "%"+createTime+"%");
    	
    	return query.getResultList();
    }
	public void insert(Accommodation accommodation) {
		Transaction tx = session.beginTransaction();
		try {

			session.save(accommodation);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
	}

	public void update(Accommodation accommodation) {
		Transaction tx = session.beginTransaction();
		try {

			session.update(accommodation);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}

	}

	public void delete(Accommodation accommodation) {
		Transaction tx = session.beginTransaction();
		try {

			session.delete(accommodation);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
	}

}
